#!/usr/bin/env python3

#
# Copyright 2016 HNSDFZ
#

import os
import sys
import json
import re

from bs4 import BeautifulSoup

NO_NETWORK = False

time_regex = re.compile("(\d|\d+.\d+)\ss")
memory_regex = re.compile("(\d+)\sMB")

template = None
with open("./template.html") as file:
    template = file.read()


def download(file, save_to):
    os.system("wget \"{}\" -O \"{}\" -nv".format(file, save_to))


def parse(idx):
    global time_regex
    global memory_regex
    global template

    print("(info) Parsing {}...".format(idx))

    try:
        html = "./cogs/{}/page.html".format(idx)
        document = None
        with open(html, "rb") as file:
            document = BeautifulSoup(
                file.read().decode("utf-8", "ignore"), "lxml"  # Require lxml for faster parsing
            )

        info = {
            "name": None,                      # string
            "source_ext": ".cpp",
            "build_file": "a.out",
            "compiler": "g++ -O2 -std=c++11",
            "start_id": 1,
            "end_id": None,                    # int
            "name_format": "{0}{1}.{2}",
            "time_limit": None,                # float
            "memory_limit": None,              # float
            "special_judge": False,
            "tags": None                       # list
        }

        title = document.title
        head = document.select_one("div.tou")
        tags = [x.text for x in document.select("#fenlei > a")]
        statement = document.dl

        info["title"] = title.text.split(" - ")[0]
        info["name"] = document.select_one("div.tou > code").text.split(".")[0]
        info["end_id"] = int(document.select_one("span.badge").text)
        info["time_limit"] = float(time_regex.findall(head.text)[0])
        info["memory_limit"] = float(memory_regex.findall(head.text)[0])
        info["tags"] = tags

        config = "./cogs/{}/{}.json".format(idx, info["name"])
        problem = "./cogs/{}/{}.html".format(idx, info["name"])

        # Download images
        if not NO_NETWORK:
            images = document.select_one("#probdetail").select("img")
            if len(images) > 0:
                for image in images:
                    url = image["src"]
                    filename = os.path.basename(url)
                    image["src"] = filename

                    download(
                        "http://218.28.19.228/{}".format(url),
                        "./cogs/{}/{}".format(idx, filename)
                    )

        with open(config, "w") as file:
            file.write(json.dumps(info, indent=4, ensure_ascii=False))
        with open(problem, "w") as file:
            file.write(
                template.format(
                    title = title.text,
                    head = head,
                    tags = "TAGS: {}".format(" / ".join(tags)),
                    statement = statement
                )
            )

    except Exception as e:
        print("(error) Error when parsing {}: {}".format(idx, str(e)))

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: {} [START] [END] [--no-network]".format(sys.argv[0]))
        exit(-1)

    if len(sys.argv) > 3:
        if sys.argv[3] == "--no-network":
            NO_NETWORK = True

    start = int(sys.argv[1])
    end = int(sys.argv[2])

    for idx in range(start, end + 1):
        parse(idx)
